package cn.ultrarisk.scadmin.manager.biz.solr.service;

import cn.ultrarisk.mybatis.extension.query.Query;
import cn.ultrarisk.mybatis.extension.query.Updater;
import cn.ultrarisk.scadmin.manager.biz.common.context.OperationType;
import cn.ultrarisk.scadmin.manager.biz.common.service.OperationLogService;
import cn.ultrarisk.scadmin.manager.biz.solr.dao.SolrNodeDAO;
import cn.ultrarisk.scadmin.manager.biz.solr.entity.SolrNode;
import cn.ultrarisk.scadmin.manager.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * SolrNodeService
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/5/13
 * @since V1.0
 */
@Service
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public class SolrNodeService {
    @Autowired
    private SolrNodeDAO solrNodeDAO;
    @Autowired
    private OperationLogService operationLogService;

    public SolrNode saveByZKInfo(String path, Long updateTime) {
        String name = path.split("-")[1];

        SolrNode node = solrNodeDAO.get(Query.query(SolrNode.NAME, name));
        if (node == null) {
            node = new SolrNode(name);
        }

        node.setAvailable(true);
        node.setUpdateTime(updateTime);

        if (node.getId() == null) {
            solrNodeDAO.create(node);
        } else {
            solrNodeDAO.update(node.getId(), Updater.update(SolrNode.AVAILABLE, true).addField(SolrNode.UPDATE_TIME, updateTime));
        }
        return node;
    }

    public void updateBySolrInfo(Integer id, JsonUtil.MapWrapper luceneMap, JsonUtil.MapWrapper jvmMap, JsonUtil.MapWrapper systemMap) {
        Updater updater = new Updater();
        updater.addField(SolrNode.SOLR_SPEC_VERSION, luceneMap.getStringByPath("solr-spec-version"));
        updater.addField(SolrNode.SOLR_IMPL_VERSION, luceneMap.getStringByPath("solr-impl-version"));
        updater.addField(SolrNode.LUCENE_SPEC_VERSION, luceneMap.getStringByPath("lucene-spec-version"));
        updater.addField(SolrNode.LUCENE_IMPL_VERSION, luceneMap.getStringByPath("lucene-impl-version"));

        updater.addField(SolrNode.JVM_VERSION, jvmMap.getStringByPath("version"));
        updater.addField(SolrNode.JVM_NAME, jvmMap.getStringByPath("name"));
        updater.addField(SolrNode.JVM_SPEC_VENDOR, jvmMap.getStringByPath("spec.vendor"));
        updater.addField(SolrNode.JVM_SPEC_NAME, jvmMap.getStringByPath("spec.name"));
        updater.addField(SolrNode.JVM_SPEC_VERSION, jvmMap.getStringByPath("spec.version"));
        updater.addField(SolrNode.JVM_JRE_VENDOR, jvmMap.getStringByPath("jre.vendor"));
        updater.addField(SolrNode.JVM_JRE_VERSION, jvmMap.getStringByPath("jre.version"));
        updater.addField(SolrNode.JVM_VM_VENDOR, jvmMap.getStringByPath("vm.vendor"));
        updater.addField(SolrNode.JVM_VM_NAME, jvmMap.getStringByPath("vm.name"));
        updater.addField(SolrNode.JVM_VM_VERSION, jvmMap.getStringByPath("vm.version"));
        updater.addField(SolrNode.JVM_PROCESSORS, jvmMap.getIntByPath("processors"));
        updater.addField(SolrNode.JVM_START_TIME, jvmMap.getGMTDateByPath("jmx.startTime"));
        updater.addField(SolrNode.JVM_UPTIME, jvmMap.getLongByPath("jmx.upTimeMS"));

        updater.addField(SolrNode.SYSTEM_NAME, systemMap.getStringByPath("name"));
        updater.addField(SolrNode.SYSTEM_VERSION, systemMap.getStringByPath("version"));
        updater.addField(SolrNode.SYSTEM_ARCH, systemMap.getStringByPath("arch"));
        updater.addField(SolrNode.SYSTEM_UNAME, systemMap.getStringByPath("uname"));
        updater.addField(SolrNode.SYSTEM_UPTIME, systemMap.getStringByPath("uptime"));

        solrNodeDAO.update(id, updater);
    }

    public void disable(List<Integer> ids) {
        solrNodeDAO.update(Query.inQuery(SolrNode.ID, ids), Updater.update(SolrNode.AVAILABLE, false));
    }

    public void delete(Integer id) {
        operationLogService.createSolrNodeLog(OperationType.SOLR_NODE_DELETE, id);
        solrNodeDAO.delete(id);
    }

    /**
     * 清除过期的solrNode
     *
     * @query expiredTime
     */
    public void clearExpiredData(long expiredTime) {
        Query query = Query.ltQuery(SolrNode.UPDATE_TIME, expiredTime);
        solrNodeDAO.delete(query);
    }
}
